#!/usr/local/bin/python
# -*- coding: utf-8 -*-

import datetime
from flask import Flask, url_for, request, session, jsonify, render_template, redirect
from json import dumps

from bompy import app
from utils import q, table, login_required, get2parameters, formtable, textinput, unitoptions, designoptions, typeoptions
from model import BOM


@app.route('/bom')
@login_required
@get2parameters
def bom(edit=None, IDP=None, IDC=None):
    f = ""
    editing = False
    if edit:
        editing = True
        b = BOM.getCurrent(IDP,IDC).main()
        
        form = (
        ("Parent ID", textinput("IDP",b.ID_Parent,readonly=True)),
        ("Child ID", textinput("IDC",b.ID_Child,readonly=True)),
        ("Quantity", textinput("QU",str(b.Quantity))),
        ("Unit", textinput("UN",b.unitdesc())),
        ("Start date", textinput("SD",b.Start_Date,"datepicker"))
        )
        hidden=("""<input type="hidden" name="mode" value="edit">""",)
    else:
        editing = False
        form=()#TODO poner que es readonly, y que no depende si agregas padre o hijo        
#        form = (
#        ("Parent ID", textinput("IDP",b.ID_Parent,readonly=True)),
#        ("Child ID", textinput("IDC",b.ID_Child,readonly=True)),
#        ("Quantity", textinput("QU",str(b.Quantity))),
#        ("Unit", textinput("UN",b.unitdesc())),
#        ("Start date", textinput("SD",b.Start_Date,"datepicker"))
#        )
        hidden=("""<input type="hidden" name="mode" value="edit">""",)
        
    f=formtable(form,hidden,"_bom_db")
    t=table(["Parent ID","Description","Type","Child ID","Description","Type","Quantity","Unit","Created","By user","Updated","By user","","","",""])
    
    return render_template("bom.html",f=f, editing=editing, t=t)
  
  
@app.route('/_bom_search')
def bom_search():
    d={"aaData":[]}
    q = BOM.query.all()
    for i in q:
        if i.isCurrent() and i.isMain():
            d["aaData"].append(i.table())
    return dumps(d)


@app.route('/_bom_db')
@get2parameters
def bom_db(mode, IDP, IDC, QU=None, UN=None):
    
    
    pass
